from setting import *
from gamebackground import *
from boss import *
from heroplane import *


class MainScene(object):
    #初始化
    def __init__(self):
        pygame.mixer.pre_init()
        pygame.init()
        pygame.mixer.init()
        pygame.font.init()
        # pygame.mouse.set_visible(False)
        self.scene = pygame.display.set_mode((SCENEWIDTH, SCENEHEIGHT), 1, 32)
        # self.scene = pygame.display.set_mode((SCENEWIDTH, SCENEHEIGHT), pygame.FULLSCREEN, 32)
        pygame.display.set_caption("自学飞机大战--精灵组版")
        #载入地图
        self.bground = Background(self.scene)
        # Background(vect(0, 0))  # from (0,0),lefttop coordinate
        BossPlane()
        self.hero = HeroPlane()
        self.hero.set_pos(SCENEWIDTH // 2 - 50, SCENEHEIGHT - 100)

    #绘制各个元素
    def draw(self):
        self.bground.draw()
        allgroup.draw(self.scene)

    def update(self):
        self.bground.update()
        allgroup.update()

    def collideEvent(self):
        # hero_beshot = pygame.sprite.spritecollide(self.hero,bossbulletgroup,True)
        # hero_bu = pygame.sprite.spritecollide(self.hero,bulletgroup,True)
        boss_bullet_hero = pygame.sprite.groupcollide(
            herogroup, bossbulletgroup, False, True,
            pygame.sprite.collide_mask)
        boss_missle_hero = pygame.sprite.groupcollide(
            herogroup, bossmisslegroup, False, True,
            pygame.sprite.collide_mask)
        boss_gmissle_hero = pygame.sprite.groupcollide(
            herogroup, bossGMgroup, False, True,
            pygame.sprite.collide_mask)
        # if hero_beshot_boss:
        #     print(hero_beshot_boss)
        # pass

    #处理键盘和鼠标相应
    def handleEvent(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_j:
                    #普通射击
                    pass
                elif event.key == pygame.K_a:
                    self.hero.key_down(pygame.K_a)
                elif event.key == pygame.K_d:
                    self.hero.key_down(pygame.K_d)
                elif event.key == pygame.K_w:
                    self.hero.key_down(pygame.K_w)
                elif event.key == pygame.K_s:
                    self.hero.key_down(pygame.K_s)
                #ESC 退出
                elif event.key == pygame.K_ESCAPE or event.key == pygame.K_F4:
                    pygame.quit()
                    exit()
            #按键松开
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_j:
                    pass
                elif event.key == pygame.K_a:
                    self.hero.key_up(pygame.K_a)
                elif event.key == pygame.K_d:
                    self.hero.key_up(pygame.K_d)
                elif event.key == pygame.K_w:
                    self.hero.key_up(pygame.K_w)
                elif event.key == pygame.K_s:
                    self.hero.key_up(pygame.K_s)
        self.hero.press_move()

    def runScene(self):
        pygame.mixer.music.load('music/bgm.mp3')
        pygame.mixer.music.play(-1)
        clock = pygame.time.Clock()
        while True:
            clock.tick(60)
            self.draw()
            self.update()
            self.collideEvent()
            self.handleEvent()
            pygame.display.flip()


if __name__ == "__main__":
    play = MainScene()
    play.runScene()

else:
    print("i was imported by", __name__)
